﻿using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WoodRobotics.Weather.UserControls
{
    
    /// <summary>
    /// Interaction logic for TaskTrayIcon.xaml
    /// </summary>
    public partial class WPFNotifyIcon : System.Windows.Controls.UserControl
    {
        
        #region Constructor
        public WPFNotifyIcon()
        {
            
            InitializeComponent();
            
            m_TaskTrayIcon = new NotifyIcon();
            m_TaskTrayIcon.Click += new EventHandler(m_TaskTrayIcon_Click);
            
        }
        #endregion
        
        #region Fields
        private NotifyIcon m_TaskTrayIcon;
        #endregion
        
        #region Properties
        public static DependencyProperty IconProperty = DependencyProperty.Register("TaskTrayIcon", typeof(ImageSource), typeof(WPFNotifyIcon));
        public static DependencyProperty ToolTipTextProperty = DependencyProperty.Register("TaskTrayToolTip", typeof(string), typeof(WPFNotifyIcon));
        public static DependencyProperty BalloonTextProperty = DependencyProperty.Register("TaskTrayBalloonText", typeof(string), typeof(WPFNotifyIcon));
        public static DependencyProperty BalloonTitleProperty = DependencyProperty.Register("TaskTrayBalloonTitle", typeof(string), typeof(WPFNotifyIcon));
        public static DependencyProperty VisibleProperty = DependencyProperty.Register("TaskTrayVisible", typeof(bool), typeof(WPFNotifyIcon));
        
        public ImageSource TaskTrayIcon
        {
            get { return (ImageSource)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }
        public string TaskTrayToolTip
        {
            get { return (string)GetValue(ToolTipTextProperty); }
            set { SetValue(ToolTipTextProperty, value); }
        }
        public string TaskTrayBalloonText
        {
            get { return (string)GetValue(BalloonTextProperty); }
            set { SetValue(BalloonTextProperty, value); }
        }
        public string TaskTrayBalloonTitle
        {
            get { return (string)GetValue(BalloonTitleProperty); }
            set { SetValue(BalloonTitleProperty, value); }
        }
        public bool TaskTrayVisible
        {
            get { return (bool)GetValue(VisibleProperty); }
            set { SetValue(VisibleProperty, value); }
        }
        #endregion
        
        #region Events
        public event EventHandler Click;
        
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            
            base.OnPropertyChanged(e);
            
            try
            {
                if (e.Property.Name == "TaskTrayIcon")
                {
                    if (((BitmapImage)e.NewValue).IsDownloading)
                        ((ImageSource)e.NewValue).Changed += new EventHandler(WPFNotifyIcon_Changed);
                    else
                    {
                        Icon l_OrigIcon = m_TaskTrayIcon.Icon;
                        try
                        {
                            m_TaskTrayIcon.Icon = System.Drawing.Icon.FromHandle(BitmapImageToBitmap((ImageSource)e.NewValue).GetHicon());
                        }
                        catch
                        {
                            m_TaskTrayIcon.Icon = l_OrigIcon;
                        }
                    }
                }
                else if (e.Property.Name == "TaskTrayToolTip")
                    m_TaskTrayIcon.Text = (string)e.NewValue;
                else if (e.Property.Name == "TaskTrayBalloonText")
                    m_TaskTrayIcon.BalloonTipText = (string)e.NewValue;
                else if (e.Property.Name == "TaskTrayBalloonTitle")
                    m_TaskTrayIcon.BalloonTipTitle = (string)e.NewValue;
                else if (e.Property.Name == "TaskTrayVisible")
                    m_TaskTrayIcon.Visible = (bool)e.NewValue;
            }
            catch { }
            
        }
        
        private void WPFNotifyIcon_Changed(object sender, EventArgs e)
        {
            Icon l_OrigIcon = m_TaskTrayIcon.Icon;
            try
            {
                m_TaskTrayIcon.Icon = System.Drawing.Icon.FromHandle(BitmapImageToBitmap((ImageSource)sender).GetHicon());
            }
            catch
            {
                m_TaskTrayIcon.Icon = l_OrigIcon;
            }
        }
        private void m_TaskTrayIcon_Click(object sender, EventArgs e)
        {
            if (Click != null)
                Click(sender, e);
        }
        #endregion
        
        #region Methods
        private Bitmap BitmapImageToBitmap(ImageSource Original)
        {
            
            if (Original == null)
                return (Bitmap)new Bitmap(1, 1);
              
            MemoryStream l_NewMS = new MemoryStream();
            BmpBitmapEncoder l_BBE = new BmpBitmapEncoder();
            l_BBE.Frames.Add(BitmapFrame.Create((BitmapSource)Original));
            l_BBE.Save(l_NewMS);
            l_NewMS.Seek(0, SeekOrigin.Begin);

            Bitmap l_ReturnBM = (Bitmap)Bitmap.FromStream(l_NewMS);

            l_ReturnBM.MakeTransparent(System.Drawing.Color.White);

            return l_ReturnBM;
            
        }
        #endregion
        
    }
    
}